@echo off
chcp 65001 >nul
echo === 设备指令下发API - cURL测试 ===

REM API配置
set API_KEY=ak_3d97ebf664db97f3023b336f5fc0ad23
set SECRET_KEY=e61b4d35cf2a29e46e08e36dd075ea585614cd91470719c4411d2b946f677e12
set BASE_URL=http://localhost:8000

echo API Key: %API_KEY%
echo Base URL: %BASE_URL%
echo.

REM 测试1: 设备重启
echo === 测试1: 设备重启 ===
set METHOD=POST
set PATH=/api/device/command
set BODY={"device_id":"1C3426522542","commands":{"cmds":[{"cmd_name":"reboot"}]}}

REM 生成时间戳
for /f %%i in ('php -r "echo time();"') do set TIMESTAMP=%%i

REM 构建待签名字符串并生成签名
for /f %%i in ('php -r "echo hash_hmac('sha256', '%METHOD%\n%PATH%\n%TIMESTAMP%\n%BODY%', '%SECRET_KEY%');"') do set SIGNATURE=%%i

echo 请求体: %BODY%
echo 时间戳: %TIMESTAMP%
echo 签名: %SIGNATURE%
echo.

curl -X POST "%BASE_URL%%PATH%" ^
  -H "Authorization: ApiKey %API_KEY%" ^
  -H "Timestamp: %TIMESTAMP%" ^
  -H "Signature: %SIGNATURE%" ^
  -H "Content-Type: application/json" ^
  -H "X-Request-ID: req_%TIMESTAMP%" ^
  -d "%BODY%" ^
  -w "\n状态码: %%{http_code}\n响应时间: %%{time_total}s\n\n"

echo ----------------------------------------

REM 测试2: 热点配置
echo === 测试2: 热点配置 ===
set BODY={"device_id":"1C3426522542","commands":{"cmds":[{"cmd_name":"hotspot_modify","cmd_params":{"hotspot_name":"TestWiFi","hotspot_password":"12345678","hotspot_connected_num_max":10,"hotspot_whether_hide":0,"hotspot_band":1}}]}}

REM 生成新的时间戳和签名
for /f %%i in ('php -r "echo time();"') do set TIMESTAMP=%%i
for /f %%i in ('php -r "echo hash_hmac('sha256', '%METHOD%\n%PATH%\n%TIMESTAMP%\n%BODY%', '%SECRET_KEY%');"') do set SIGNATURE=%%i

echo 请求体: %BODY%
echo 时间戳: %TIMESTAMP%
echo 签名: %SIGNATURE%
echo.

curl -X POST "%BASE_URL%%PATH%" ^
  -H "Authorization: ApiKey %API_KEY%" ^
  -H "Timestamp: %TIMESTAMP%" ^
  -H "Signature: %SIGNATURE%" ^
  -H "Content-Type: application/json" ^
  -H "X-Request-ID: req_%TIMESTAMP%" ^
  -d "%BODY%" ^
  -w "\n状态码: %%{http_code}\n响应时间: %%{time_total}s\n\n"

echo ----------------------------------------

REM 测试3: SIM卡切换
echo === 测试3: SIM卡切换 ===
set BODY={"device_id":"1C3426522542","commands":{"cmds":[{"cmd_name":"sim_switch","cmd_params":{"sim_id":1}}]}}

for /f %%i in ('php -r "echo time();"') do set TIMESTAMP=%%i
for /f %%i in ('php -r "echo hash_hmac('sha256', '%METHOD%\n%PATH%\n%TIMESTAMP%\n%BODY%', '%SECRET_KEY%');"') do set SIGNATURE=%%i

echo 请求体: %BODY%
echo 时间戳: %TIMESTAMP%
echo 签名: %SIGNATURE%
echo.

curl -X POST "%BASE_URL%%PATH%" ^
  -H "Authorization: ApiKey %API_KEY%" ^
  -H "Timestamp: %TIMESTAMP%" ^
  -H "Signature: %SIGNATURE%" ^
  -H "Content-Type: application/json" ^
  -H "X-Request-ID: req_%TIMESTAMP%" ^
  -d "%BODY%" ^
  -w "\n状态码: %%{http_code}\n响应时间: %%{time_total}s\n\n"

echo ----------------------------------------

REM 测试4: 查看支持的指令（GET请求）
echo === 测试4: 查看支持的指令 ===
set METHOD=GET
set PATH=/api/device/commands
set BODY=

for /f %%i in ('php -r "echo time();"') do set TIMESTAMP=%%i
for /f %%i in ('php -r "echo hash_hmac('sha256', '%METHOD%\n%PATH%\n%TIMESTAMP%\n%BODY%', '%SECRET_KEY%');"') do set SIGNATURE=%%i

echo GET请求: %BASE_URL%%PATH%
echo 时间戳: %TIMESTAMP%
echo 签名: %SIGNATURE%
echo.

curl -X GET "%BASE_URL%%PATH%" ^
  -H "Authorization: ApiKey %API_KEY%" ^
  -H "Timestamp: %TIMESTAMP%" ^
  -H "Signature: %SIGNATURE%" ^
  -H "Content-Type: application/json" ^
  -w "\n状态码: %%{http_code}\n响应时间: %%{time_total}s\n\n"

echo ----------------------------------------

REM 测试5: 查看指令示例
echo === 测试5: 查看指令示例 ===
set PATH=/api/device/command/examples

for /f %%i in ('php -r "echo time();"') do set TIMESTAMP=%%i
for /f %%i in ('php -r "echo hash_hmac('sha256', '%METHOD%\n%PATH%\n%TIMESTAMP%\n%BODY%', '%SECRET_KEY%');"') do set SIGNATURE=%%i

echo GET请求: %BASE_URL%%PATH%
echo 时间戳: %TIMESTAMP%
echo 签名: %SIGNATURE%
echo.

curl -X GET "%BASE_URL%%PATH%" ^
  -H "Authorization: ApiKey %API_KEY%" ^
  -H "Timestamp: %TIMESTAMP%" ^
  -H "Signature: %SIGNATURE%" ^
  -H "Content-Type: application/json" ^
  -w "\n状态码: %%{http_code}\n响应时间: %%{time_total}s\n\n"

echo === 测试完成 ===
pause
